package com.bkgy.controller.continuousSlantingHistory.utils;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Map;

public class FieldMappingUtil {
    private static final Map<String, String> FIELD_MAPPINGS = new HashMap<>();

    static {
        // 初始化字段映射关系
        // CPC 字段
        for (int i = 1; i <= 17; i++) {
            if (i != 9) {
                FIELD_MAPPINGS.put("cpc" + i + "Avg", "cpc" + i + "_avg");
                FIELD_MAPPINGS.put("cpc" + i + "Std", "cpc" + i + "_std");
                FIELD_MAPPINGS.put("cpc" + i + "Max", "cpc" + i + "_max");
                FIELD_MAPPINGS.put("cpc" + i + "Min", "cpc" + i + "_min");
                FIELD_MAPPINGS.put("cpc" + i + "StripAvg", "cpc" + i + "_strip_avg");
                FIELD_MAPPINGS.put("cpc" + i + "StripStd", "cpc" + i + "_strip_std");
            }
        }

        // CPC9 特殊字段
        for (int j = 0; j <= 8; j++) {
            FIELD_MAPPINGS.put("cpc9" + j + "Avg", "cpc9_" + j + "_avg");
            FIELD_MAPPINGS.put("cpc9" + j + "Std", "cpc9_" + j + "_std");
            FIELD_MAPPINGS.put("cpc9" + j + "Max", "cpc9_" + j + "_max");
            FIELD_MAPPINGS.put("cpc9" + j + "Min", "cpc9_" + j + "_min");
            FIELD_MAPPINGS.put("cpc9" + j + "StripAvg", "cpc9_" + j + "_strip_avg");
            FIELD_MAPPINGS.put("cpc9" + j + "StripStd", "cpc9_" + j + "_strip_std");
        }

        // EPC 字段
        for (int k = 1; k <= 2; k++) {
            FIELD_MAPPINGS.put("epc" + k + "Avg", "epc" + k + "_avg");
            FIELD_MAPPINGS.put("epc" + k + "Std", "epc" + k + "_std");
            FIELD_MAPPINGS.put("epc" + k + "StripAvg", "epc" + k + "_strip_avg");
            FIELD_MAPPINGS.put("epc" + k + "StripStd", "epc" + k + "_strip_std");
        }
    }

    public static void copyProperties(Object source, Object target) {
        try {
            Class<?> sourceClass = source.getClass();
            Class<?> targetClass = target.getClass();

            for (Map.Entry<String, String> entry : FIELD_MAPPINGS.entrySet()) {
                String sourceField = entry.getKey();
                String targetField = entry.getValue();

                // 获取getter方法
                String getterName = "get" + sourceField.substring(0, 1).toUpperCase() + sourceField.substring(1);
                Method getter = sourceClass.getMethod(getterName);

                // 获取setter方法
                String setterName = "set" + targetField.substring(0, 1).toUpperCase() + targetField.substring(1);
                Method setter = targetClass.getMethod(setterName, getter.getReturnType());

                // 执行拷贝
                Object value = getter.invoke(source);
                if (value != null) {
                    setter.invoke(target, value);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
